package com.qianfeng.controller;

import com.alibaba.fastjson.JSON;
import com.qianfeng.Service.MemberService;
import com.qianfeng.model.Member;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/login")
public class LoginController extends HttpServlet {
    private MemberService memberService=new MemberService();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        Map<String,Object> r=new HashMap<>();
        try {
        String phone=request.getParameter("username");
        String password=request.getParameter("password");

        Member member =memberService.login(phone,password);
        request.getSession().setAttribute("sessionUser",member);
        r.put("code",0);
        r.put("msg","登录成功");
        } catch (Exception e) {
            e.printStackTrace();
            r.put("code",1);
            r.put("msg",e.getMessage());
        }
        response.getWriter().write(JSON.toJSONString(r));

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
